package com.unknownman.block.controller.admin;

import com.github.pagehelper.PageInfo;
import com.unknownman.block.service.TTokenErc20Service;
import com.unknownman.block.service.TTokenSwapRecordService;
import com.unknownman.block.vo.ERC20HourHoldVO;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import org.cloud.annotation.SystemResource;
import org.cloud.mongo.MongoEnumVO.DataType;
import org.cloud.mongo.MongoEnumVO.MongoOperatorEnum;
import org.cloud.mongo.MongoEnumVO.RelationalOperator;
import org.cloud.mongo.MongoQueryParam;
import org.cloud.mongo.MongoQueryParamsDTO;
import org.cloud.utils.mongo.MongoDBUtil;
import org.cloud.vo.JavaBeanResultMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Validated
@RestController
@RequestMapping("/admin/report")
@SystemResource(path = "/admin/report")
@Slf4j
public class ReportController {

    @Autowired
    TTokenErc20Service tTokenErc20Service;

    /**
     * 查询erc20持仓数据，
     *
     * @param queryParamsDTO 查询条件
     * @param type           查询类型，day，按天，hour按小时
     * @return
     */
    @PostMapping("/queryErc20Hold")
    List<ERC20HourHoldVO> queryErc20Hold(@RequestBody MongoQueryParamsDTO queryParamsDTO, @RequestParam("queryType") String type,
        @RequestParam("tokenAddress") String tokenAddress, @RequestParam(name = "limit", required = false) Long limit)
        throws Exception {

        if ("day".equals(type)) {
            MongoQueryParam queryParam = new MongoQueryParam();
            queryParam.setName("currentHour");
            queryParam.setOperator(MongoOperatorEnum.IS);
            queryParam.setValue(23);
            queryParam.setDataType(DataType.Integer);
            queryParam.setRelationalOperator(RelationalOperator.AND);
            queryParamsDTO.getParams().add(queryParam);
        }

        List<ERC20HourHoldVO> resultList;

        if (limit != null) {
            PageInfo<ERC20HourHoldVO> result = MongoDBUtil.single()
                .paged(1L, limit, queryParamsDTO, ERC20HourHoldVO.class, "ERC20HourHoldCollection");
            resultList = result.getList();
        } else {
            resultList = MongoDBUtil.single().list( queryParamsDTO, ERC20HourHoldVO.class, "ERC20HourHoldCollection");
        }

        Collections.reverse(resultList);
        if ("day".equals(type)) {
            resultList.addAll(tTokenErc20Service.queryTokenErc20ByHour(tokenAddress));
        }
        return resultList;
    }


    @PostMapping("/countTokenTotalByOwner")
    List<JavaBeanResultMap> queryErc20Hold(@RequestBody Map<String, Object> params) throws Exception {
        return tTokenErc20Service.countTokenTotalByOwner(params);
    }

    @Autowired
    TTokenSwapRecordService tokenSwapRecordService;

    @PostMapping("/countSwapResultByDay")
    List<JavaBeanResultMap> countSwapResultByDay(@RequestBody Map<String, Object> params) throws Exception {
        return tokenSwapRecordService.countSwapResultByDay(params);
    }


}
